//
//  WithFormButtonsBar.m
//  MClient
//
//  Created by zhouyi on 10-12-13.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "WithFormButtonsBar.h"


@implementation WithFormButtonsBar

@synthesize withFormButtonsBarHeight;

- (id)initWithFrame:(CGRect)frame {
    if ((self = [super initWithFrame:frame])) {
        withFormButtonsBarHeight = frame.size.height;
		self.autoresizingMask = nMCViewAutoresizingMask;
    }
    return self;
}

- (void) addWithFormButtons:withFormButtons
				  buttonCol:(int) buttonCol
				   delegate:delegate {
	float buttonSpace = 20.0;
	float oneButtonWidth = (CGRectGetWidth(self.bounds) - buttonSpace * (buttonCol + 1))/buttonCol;
	float startX = buttonSpace;
	
	float buttonHeigth = 34.0;
	int allButtonsRow = withFormButtonsBarHeight/nWithFormButtonsBarHeight;
	float heightSpace = (withFormButtonsBarHeight - allButtonsRow * buttonHeigth)/(allButtonsRow + 1);
	float startY = heightSpace;
	
	for (int i = 0; i < [withFormButtons count]; i++) {
		NSMutableDictionary *withFormButton = [withFormButtons objectAtIndex:i];
		int colspan = 1;
		UIColor *fontColor = [MCUserDefaults instance].skin_withFormButtonFontColor;
		float fontSize = 16.0f;
		if ([withFormButton objectForKey:@"style"]) {
			NSMutableDictionary *style = [withFormButton objectForKey:@"style"];
			if ([style objectForKey:@"colspan"]) {
				colspan = [[style objectForKey:@"colspan"] intValue];
				if (colspan > buttonCol) {
					colspan = buttonCol;
				}
				if (colspan == 0) {
					colspan = 1;
				}
			}
			if ([style objectForKey:@"font_color"]) {
				fontColor = [MCFunction hexColor:[NSString stringWithFormat:@"%@64", [style objectForKey:@"font_color"]]];
			}
			if ([style objectForKey:@"font_size"]) {
				fontSize = [[style objectForKey:@"font_size"] floatValue]/0.75;
			}
		}
		UIButton* button = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
		button.backgroundColor = [UIColor clearColor];
		UIImage *buttonBg = [MCUserDefaults instance].skin_withFormButtonBg;
		UIImage *stretchableButtonBg = [buttonBg stretchableImageWithLeftCapWidth:12 topCapHeight:5];
		[button setBackgroundImage:stretchableButtonBg forState:UIControlStateNormal];
		
		int buttonWidth = colspan * oneButtonWidth + (colspan - 1) * buttonSpace;
		if (startX + buttonWidth > CGRectGetWidth(self.bounds)) {
			startX = buttonSpace;
			startY = startY + buttonHeigth + heightSpace;
		}
		button.frame = CGRectMake(startX, startY, buttonWidth, buttonHeigth);
		startX = startX + buttonWidth + buttonSpace;
		
		[button setTitle:[withFormButton objectForKey:@"lbl"] forState:UIControlStateNormal];
		[button setTitleColor:fontColor forState:UIControlStateNormal];
		button.titleLabel.font = [UIFont boldSystemFontOfSize:fontSize];
		button.tag = i;
		[button addTarget:delegate action:@selector(withFormButtonsAction:) forControlEvents:UIControlEventTouchUpInside];
		[self addSubview:button];
		[button release];
	}	
	if ([withFormButtons count] == 1) {
		UIButton *button = (UIButton *)[self viewWithTag:0];
		button.frame = CGRectMake((CGRectGetWidth(self.bounds) - CGRectGetWidth(button.bounds))/2, button.frame.origin.y, CGRectGetWidth(button.bounds), CGRectGetHeight(button.bounds));
	}
}

- (void)dealloc {
    [super dealloc];
}


@end
